//
//  RGInviteVC.m
//  RedGrass
//
//  Created by wuyine on 2019/2/13.
//  Copyright © 2019年 hongcaosp. All rights reserved.
//

#import "RGInviteVC.h"
#import "RGInviteSecondVC.h"
#import "RGMeInterfaceBulider.h"
@interface RGInviteVC ()
@property (nonatomic,strong) UITextField *textField;
@property (nonatomic,strong) UIButton *submitBtn;
@property (nonatomic,strong) UILabel *skipBtn;
@property (nonatomic,strong) UILabel *descLabel;
@property (nonatomic,strong) UIView *succTipView;
@end

@implementation RGInviteVC

- (void)viewDidLoad {
    [super viewDidLoad];
//    self.customNavBar.hidden = YES;
    self.NavStyleType = RGCustomNavStyleType_default;
//    self.customNavBar.backgroundColor = [UIColor clearColor];
    [self showBackButton];
    [self initUI];
}

- (void)initUI {
    UIImageView *bg = [[UIImageView alloc] initWithImage:IMAGE_NAMED(@"二维码_bj")];
    bg.userInteractionEnabled = YES;
    [self.customContentView addSubview:bg];
    [bg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsZero);
//        make.top.mas_equalTo(-SafeAreaTopHeight);
    }];
    
    UIScrollView *scroll = [[UIScrollView alloc] init];
    scroll.backgroundColor = [UIColor clearColor];
    [self.customContentView addSubview:scroll];
    [scroll mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsZero);
//        make.top.mas_equalTo(-SafeAreaTopHeight);
    }];
    
    UIView *canzhao = [[UIView alloc] init];
    [scroll addSubview:canzhao];
    [canzhao mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(scroll.mas_width);
        make.height.mas_equalTo(0.5);
        make.leading.trailing.top.mas_equalTo(0);
    }];
    
    UIImageView *head = [[UIImageView alloc] initWithImage:IMAGE_NAMED(@"邀请大礼包")];
    [scroll addSubview:head];
    CGFloat rat = KScreenWidth / 375.0;
    [head mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.mas_equalTo(0);
        make.top.mas_equalTo(0/*KIsiPhoneX?44:22*/);
        make.size.mas_equalTo(CGSizeMake(372 * rat, 158 * rat));
    }];
    
    [scroll addSubview:self.textField];
    [_textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(head.mas_bottom).offset(30);
        make.leading.mas_equalTo(15);
        make.trailing.mas_equalTo(-15);
        make.height.mas_equalTo(48);
    }];
    
    [scroll addSubview:self.submitBtn];
    [_submitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(15);
        make.trailing.mas_equalTo(-15);
        make.top.equalTo(self.textField.mas_bottom).offset(30);
        make.height.mas_equalTo(48);
    }];
    
    [scroll addSubview:self.skipBtn];
    [_skipBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.top.equalTo(self.submitBtn.mas_bottom).offset(30);
    }];
    
    [scroll addSubview:self.descLabel];
    [_descLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(15);
        make.trailing.mas_equalTo(-15);
        make.top.equalTo(self.skipBtn.mas_bottom).offset(40);
        make.bottom.mas_equalTo(-20+KIsiPhoneX?-34:0);
    }];
}

- (void)submit:(UIButton *)btn {
    HttpRequest *quest = [HttpRequest buildRequest:url_userDynamic];
    NSDictionary *params = [RGMeInterfaceBulider buildInvitationCode:[RGUserInfoModel getUserInfo].invitationCode];
    AA_WEAK_MAKE(self) weakSelf = self;
    [quest post:params success:^(id data) {
        [weakSelf showSuccTip];
    }fail:^(NSInteger code, NSString *message) {
        [weakSelf showTextMessageWithMessage:@"邀请码错误"];
    }];
}

- (void)skip {
    RGInviteSecondVC *vc = [[RGInviteSecondVC alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)ensure:(UIButton *)btn {
    RGInviteSecondVC *vc = [[RGInviteSecondVC alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)showSuccTip {
    [self.zh_popupController presentContentView:self.succTipView];
}

- (UITextField *)textField {
    if (!_textField) {
        _textField = [[UITextField alloc] init];
        _textField.layer.cornerRadius = 24;
        _textField.layer.masksToBounds = YES;
        _textField.layer.borderColor = UICOLOR_WITH_RGBINT(0xffffff).CGColor;
        _textField.layer.borderWidth = 1;
        _textField.placeholder = @"请输入好友的邀请码";
        _textField.textColor = UICOLOR_WITH_RGBINT(0xffffff);
        _textField.font = FONT(customFontName, 16);
         [_textField setValue:UICOLOR_WITH_RGBINT(0x999999) forKeyPath:@"_placeholderLabel.textColor"];
        _textField.textAlignment = NSTextAlignmentCenter;
    }
    return _textField;
}

- (UIButton *)submitBtn {
    if (!_submitBtn) {
        _submitBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_submitBtn setTitle:@"马上提交" forState:UIControlStateNormal];
        _submitBtn.layer.cornerRadius = 24;
        _submitBtn.backgroundColor = UICOLOR_WITH_RGBINT(0xF43556);
        [_submitBtn addTarget:self action:@selector(submit:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _submitBtn;
}

- (UILabel *)skipBtn {
    if (!_skipBtn) {
        _skipBtn = [[UILabel alloc] init];
        _skipBtn.userInteractionEnabled = YES;
        _skipBtn.textColor = UICOLOR_WITH_RGBINT(0xffffff);
        ///定义属性字符串
        NSMutableAttributedString *att  = [[NSMutableAttributedString alloc]initWithString:@"点击跳过，我要邀请好友赚红包~"];
        // 下划线
        [att addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:NSMakeRange(0, att.length)];
        _skipBtn.attributedText = att;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(skip)];
        [_skipBtn addGestureRecognizer:tap];
        
    }
    return _skipBtn;
}

- (UILabel *)descLabel {
    if (!_descLabel) {
        _descLabel = [[UILabel alloc] init];
        _descLabel.textColor = UICOLOR_WITH_RGBINT(0xffffff);
        _descLabel.text = @"温馨提示：\n1.下载软件后的10天内可以输入邀请码，超过时间不能输入。\n2.一个手机只能输入一次邀请码。已输入过邀请码的手机，切换账号也无法输入其他邀请码。\n3.使用App分身软件、高危手机号等非正常用户不能输入邀请码，且会被系统记录。";
        _descLabel.numberOfLines = 0;
    }
    return _descLabel;
}

- (UIView *)succTipView {
    if (!_succTipView) {
        _succTipView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 250, 169)];
        _succTipView.layer.cornerRadius = 10;
        _succTipView.backgroundColor = UICOLOR_WITH_RGBINT(0xffffff);
        UIImageView *succImg = [[UIImageView alloc] initWithImage:IMAGE_NAMED(@"验证成功")];
        [_succTipView addSubview:succImg];
        [succImg mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(0);
            make.top.mas_equalTo(20);
            make.size.mas_equalTo(CGSizeMake(28, 28));
        }];
        
        UILabel *tipLabel = [[UILabel alloc] init];
        tipLabel.text = @"验证成功\n我也去邀请好友~";
        tipLabel.textAlignment = NSTextAlignmentCenter;
        tipLabel.textColor = UICOLOR_WITH_RGBINT(0x333333);
        tipLabel.font = FONT(customFontName, 15);
        tipLabel.numberOfLines = 0;
        [_succTipView addSubview:tipLabel];
        [tipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(0);
            make.top.equalTo(succImg.mas_bottom).offset(10);
        }];
        
        UIButton *submitBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        submitBtn.layer.cornerRadius = 12.5;
        submitBtn.titleLabel.font = FONT(customFontName, 15);
        [submitBtn setTitle:@"确定" forState:UIControlStateNormal];
        submitBtn.backgroundColor = UICOLOR_WITH_RGBINT(0xF43556);
        [submitBtn addTarget:self action:@selector(ensure:) forControlEvents:UIControlEventTouchUpInside];
        [_succTipView addSubview:submitBtn];
        [submitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(0);
            make.size.mas_equalTo(CGSizeMake(100, 25));
            make.top.equalTo(tipLabel.mas_bottom).offset(20);
        }];
        
        
    }
    return _succTipView;
}

@end
